/*     */ package com.zimbra.cs.datasource.imap;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Pair;
/*     */ import com.zimbra.cs.account.DataSource;
/*     */ import com.zimbra.cs.mailbox.Folder;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxLock;
/*     */ import com.zimbra.cs.mailbox.Message;
/*     */ import com.zimbra.cs.mailbox.util.TypedIdList;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Collection;
/*     */ import java.util.HashSet;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ class MessageChanges
/*     */ {
/*     */   private final DataSource ds;
/*     */   private final Folder folder;
/*     */   private final Mailbox mbox;
/*     */   private List<MessageChange> changes;
/*     */   private int lastChangeId;
/*     */   
/*     */   public static MessageChanges getChanges(DataSource ds, Folder folder, int changeId)
/*     */     throws ServiceException
/*     */   {
/*  41 */     return new MessageChanges(ds, folder).findChanges(changeId);
/*     */   }
/*     */   
/*     */   private MessageChanges(DataSource ds, Folder folder) {
/*  45 */     this.ds = ds;
/*  46 */     this.folder = folder;
/*  47 */     this.mbox = folder.getMailbox();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   private MessageChanges findChanges(int changeId)
/*     */     throws ServiceException
/*     */   {
/*  55 */     this.mbox.lock.lock(false);
/*     */     List<Integer> tombstones;
/*  57 */     List<Integer> modifiedItems; try { this.lastChangeId = this.mbox.getLastChangeID();
/*  58 */       if (this.lastChangeId <= changeId) {
/*  59 */         return this;
/*     */       }
/*  61 */       tombstones = this.mbox.getTombstones(changeId).getIds(MailItem.Type.MESSAGE);
/*  62 */       modifiedItems = (List)this.mbox.getModifiedItems(null, changeId, MailItem.Type.MESSAGE).getFirst();
/*     */     } finally {
/*  64 */       this.mbox.lock.release();
/*     */     }
/*  66 */     if (((tombstones == null) || (tombstones.isEmpty())) && (modifiedItems.isEmpty())) {
/*  67 */       return this;
/*     */     }
/*     */     
/*  70 */     this.changes = new ArrayList();
/*     */     
/*     */     Iterator i$;
/*  73 */     if (tombstones != null) {
/*  74 */       for (i$ = tombstones.iterator(); i$.hasNext();) { int id = ((Integer)i$.next()).intValue();
/*  75 */         ImapMessage tracker = getTracker(id);
/*  76 */         if ((tracker != null) && (tracker.getFolderId() == this.folder.getId())) {
/*  77 */           this.changes.add(MessageChange.deleted(id, tracker));
/*     */         }
/*     */       }
/*     */     }
/*     */     
/*     */ 
/*  83 */     for (Iterator i$ = modifiedItems.iterator(); i$.hasNext();) { int id = ((Integer)i$.next()).intValue();
/*  84 */       MessageChange change = getChange(id);
/*  85 */       if (change != null) {
/*  86 */         this.changes.add(change);
/*     */       }
/*     */     }
/*     */     
/*  90 */     return this;
/*     */   }
/*     */   
/*     */   private MessageChange getChange(int msgId) throws ServiceException {
/*  94 */     Message msg = getMessage(msgId);
/*  95 */     if (msg != null) {
/*  96 */       ImapMessage tracker = getTracker(msgId);
/*  97 */       if (tracker != null) {
/*  98 */         if (msg.getFolderId() == this.folder.getId()) {
/*  99 */           if (tracker.getFolderId() != this.folder.getId())
/*     */           {
/* 101 */             return MessageChange.moved(msg, tracker);
/*     */           }
/* 103 */           if (tracker.getFlags() != msg.getFlagBitmask())
/*     */           {
/* 105 */             return MessageChange.updated(msg, tracker);
/*     */           }
/* 107 */         } else if (tracker.getFolderId() == this.folder.getId())
/*     */         {
/* 109 */           return MessageChange.moved(msg, tracker);
/*     */         }
/* 111 */       } else if (msg.getFolderId() == this.folder.getId())
/*     */       {
/* 113 */         return MessageChange.added(msg);
/*     */       }
/*     */     }
/* 116 */     return null;
/*     */   }
/*     */   
/*     */   public boolean hasChanges() {
/* 120 */     return (this.changes != null) && (!this.changes.isEmpty());
/*     */   }
/*     */   
/*     */   public Collection<MessageChange> getChanges() {
/* 124 */     if (this.changes == null) {
/* 125 */       this.changes = new ArrayList();
/*     */     }
/* 127 */     return this.changes;
/*     */   }
/*     */   
/*     */   public int getLastChangeId() {
/* 131 */     return this.lastChangeId;
/*     */   }
/*     */   
/*     */   public Collection<Integer> getFolderIdsToSync() {
/* 135 */     HashSet<Integer> folderIds = new HashSet();
/* 136 */     for (MessageChange change : this.changes) {
/* 137 */       if ((change.isAdded()) || (change.isUpdated()) || (change.isMoved())) {
/* 138 */         folderIds.add(Integer.valueOf(change.getMessage().getFolderId()));
/*     */       }
/* 140 */       if ((change.isUpdated()) || (change.isMoved()) || (change.isDeleted())) {
/* 141 */         folderIds.add(Integer.valueOf(change.getTracker().getFolderId()));
/*     */       }
/*     */     }
/* 144 */     return folderIds;
/*     */   }
/*     */   
/*     */   private Message getMessage(int msgId) throws ServiceException {
/*     */     try {
/* 149 */       return this.mbox.getMessageById(null, msgId);
/*     */     } catch (MailServiceException.NoSuchItemException e) {}
/* 151 */     return null;
/*     */   }
/*     */   
/*     */   private ImapMessage getTracker(int msgId) throws ServiceException
/*     */   {
/*     */     try {
/* 157 */       return new ImapMessage(this.ds, msgId);
/*     */     } catch (MailServiceException.NoSuchItemException e) {}
/* 159 */     return null;
/*     */   }
/*     */   
/*     */ 
/*     */   public String toString()
/*     */   {
/* 165 */     int added = 0;int updated = 0;int moved = 0;int deleted = 0;
/* 166 */     for (MessageChange change : getChanges()) {
/* 167 */       switch (change.getType()) {
/* 168 */       case ADDED:  added++; break;
/* 169 */       case UPDATED:  updated++; break;
/* 170 */       case MOVED:  moved++; break;
/* 171 */       case DELETED:  deleted++;
/*     */       }
/*     */     }
/* 174 */     return String.format("{changeId=%d,added=%d,updated=%d,moved=%d,deleted=%d}", new Object[] { Integer.valueOf(this.lastChangeId), Integer.valueOf(added), Integer.valueOf(updated), Integer.valueOf(moved), Integer.valueOf(deleted) });
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/datasource/imap/MessageChanges.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */